package feng.demo.controller.adm;

import feng.demo.entity.Order;
import feng.demo.entity.Orders;
import feng.demo.entity.User;
import feng.demo.entity.XJson;
import feng.demo.service.OrderService;
import feng.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adm/order")
public class AdmOrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;

    @GetMapping("/findAll/{p}/{l}")
    public String findAll(@PathVariable("p")Integer p,@PathVariable("l")Integer l){
        Map<String,Object> map=new HashMap<>();
        List<Orders> list = orderService.findAllsLimit(p, l);
        map.put("order",list);
        return new XJson(200,"",map).toString();
    }

    @PostMapping("/findByOrder")
    public String findByOrder(Order order){

        if (order.getUser().getPhone()!=null||order.getUser().getUname()!=null||order.getUser().getAccount()!=null){
            User user = userService.findBypwd(order.getUser());
            if(user!=null)order.setUser(user);
        }
        List<Orders> orders = orderService.findOrders(order);
        System.out.println(orders);

        return new XJson(200,"",orders).toString();
    }
}
